package com.sevennotes.classtable.ui.pages.components

import android.text.Editable
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Delete
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sevennotes.classtable.config.Config
import com.sevennotes.classtable.model.ClassRecordWithName
import com.sevennotes.classtable.model.Time
import com.sevennotes.classtable.utils.TimeUtil
import java.util.*

@Composable
fun ItemCell(
  modifier: Modifier = Modifier,
  rowHeight: Int = 60,
  hArrangement: Arrangement.Horizontal = Arrangement.Start,
  content: @Composable RowScope.() -> Unit
) {
  Row(
    modifier = modifier
      .fillMaxWidth()
      .height(rowHeight.dp)
      .padding(horizontal = 10.dp),
    horizontalArrangement = hArrangement,
    verticalAlignment = Alignment.CenterVertically,
    content = content
  )
  Divider(
    Modifier
      .fillMaxWidth()
      .height(1.dp), color = Config.dividerColor
  )
}

@Composable
fun AddItemCell(
  onClick: () -> Unit
) {
  ItemCell(
    hArrangement = Arrangement.Center,
    modifier = Modifier.height(40.dp)
  ) {
    IconButton(onClick = onClick) {
      Icon(imageVector = Icons.Filled.Add, contentDescription = "add")
    }
  }
}

@Composable
fun TimeSelectorCell(
  text: String,
  list: List<List<String>>,
  picked: List<Int>,
  onCancelClick: () -> Unit,
  onConfirmClick: (List<Int>) -> Unit,
) {
  val insidePicked = remember { mutableStateOf(MutableList(picked.size){picked[it]}) }
  AlertDialog(
    modifier = Modifier.fillMaxWidth(),
    title = { Text(text) },
    text = { TimePicker(list = list, picked = { picked[it] }, onPick = { l, n -> insidePicked.value[l] = n})},
    onDismissRequest = onCancelClick,
    buttons = {
      ConfirmAndCancelButtons(
        onConfirmClick = {onConfirmClick(insidePicked.value.toList())},
        onCancelClick = onCancelClick
      )
    }
  )
}

@Composable
fun NumberItemCell(
  editable: Boolean = false,
  onDeleteClick: () -> Unit,
  date: Date,
  note: String,
  changeNumber: Int
) {
  val isPositive = changeNumber >= 0
  ItemCell {
    AnimatedVisibility(visible = editable) {
      IconButton(onClick = onDeleteClick) {
        Icon(imageVector = Icons.Filled.Delete, contentDescription = null)
      }
    }
    Column(modifier = Modifier.weight(1f)) {
      Text(
        text = TimeUtil.formatDate(date),
        fontSize = 15.sp
      )
      Text(text = if (note == "") "无事由" else note)
    }

    Text(
      fontWeight = FontWeight.Bold,
      fontSize = 20.sp,
      text = if (isPositive) "+$changeNumber" else "$changeNumber",
      color = if (isPositive) Config.right else Config.wrong,
      modifier = Modifier.offset(x = (-12).dp, y = 8.dp)
    )
  }
}


@Composable
fun RecordCell(
  editable: Boolean = false,
  onDeleteClick: () -> Unit,
  record: ClassRecordWithName
) {
  var confirmDlg by remember { mutableStateOf(false) }
  if (confirmDlg) {
    SimpleComfirmDialog(
      text = "确定要删除这条记录吗?",
      onConfirmClick = {
        onDeleteClick()
        confirmDlg = false
      },
      onCancelClick = { confirmDlg = false }
    )
  }
  ItemCell {
    AnimatedVisibility(editable) {
      IconButton(onClick = { confirmDlg = true }) {
        Icon(imageVector = Icons.Filled.Delete, contentDescription = "delete")
      }
    }
    Text(text = record.studentName ?: "无名氏", fontSize = 19.sp, modifier = Modifier.weight(0.3f))
    Column(
      modifier = Modifier
        .weight(1f)
        .fillMaxHeight(),
      verticalArrangement = Arrangement.Center
    ) {
      Text(text = record.classRecord?.startTime?.let {
        TimeUtil.formatDate(
          it,
          "MM月dd日 HH : mm"
        )
      } + "  ${record.classRecord?.duration} 分钟")
      Text(text = record.classRecord?.note ?: "无备注")
    }
    val isPositive = record.classRecord?.gpChange?.let { it >= 0 } ?: true
    Text(
      fontWeight = FontWeight.Bold,
      fontSize = 20.sp,
      text = if (isPositive) "+${record.classRecord?.gpChange}分" else "${record.classRecord?.gpChange}分",
      color = if (isPositive) Config.right else Config.wrong,
      modifier = Modifier.offset(x = (-12).dp, y = 8.dp)
    )
  }
}